package psico_test.appCrear.gui;

import java.io.File;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.jdom.Element;

import psico_test.appCrear.appCrear;
import psico_test.appCrear.ensayo.gui.ventanaEnsayo;
import psico_test.gui.ventanaMenu;
import psico_test.utils.file.FileManager;
import psico_test.utils.xml.XMLManager;

public class ventanaCrear extends javax.swing.JFrame implements Runnable
{
	private Vector ensayos;
	private appCrear controller;
	
	public ventanaCrear(appCrear controller)
	{
		this.controller = controller;
		this.ensayos = new Vector();
	}

	public void run()
	{
		initComponents();
		crearTabla();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	private void initComponents() 
	{
		PanelTabla = new javax.swing.JPanel();
		ScrollPane = new javax.swing.JScrollPane();
		BotonExportar = new org.edisoncor.gui.button.ButtonTask();
		BotonImportar = new org.edisoncor.gui.button.ButtonTask();
		jPanel1 = new javax.swing.JPanel();
		BotonAñadir = new org.edisoncor.gui.button.ButtonIcon();
		BotonBorrar = new org.edisoncor.gui.button.ButtonIcon();
		BotonBorrarTodo = new org.edisoncor.gui.button.ButtonIcon();
		jMenuBar1 = new javax.swing.JMenuBar();
		MenuArchivo = new javax.swing.JMenu();
		MenuSalir = new javax.swing.JMenuItem();
		MenuTabla = new javax.swing.JMenu();
		MenuAdd = new javax.swing.JMenuItem();
		MenuBorrar = new javax.swing.JMenuItem();
		jSeparator1 = new javax.swing.JPopupMenu.Separator();
		MenuBorrarTodo = new javax.swing.JMenuItem();
		MenuAyuda = new javax.swing.JMenu();
		MenuAcerca = new javax.swing.JMenuItem();
		MenuInformacion = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

		PanelTabla.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Experimento", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), java.awt.Color.blue)); // NOI18N

		org.jdesktop.layout.GroupLayout PanelTablaLayout = new org.jdesktop.layout.GroupLayout(PanelTabla);
		PanelTabla.setLayout(PanelTablaLayout);
		PanelTablaLayout.setHorizontalGroup(
				PanelTablaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(PanelTablaLayout.createSequentialGroup()
						.addContainerGap()
						.add(ScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
						.addContainerGap())
		);
		PanelTablaLayout.setVerticalGroup(
				PanelTablaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(PanelTablaLayout.createSequentialGroup()
						.add(ScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
						.addContainerGap())
		);

		BotonExportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/exportar.png"))); // NOI18N
		BotonExportar.setText("Exportar");
		BotonExportar.setDescription("Generar fichero XML");
		BotonExportar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				Exportar(evt);
			}
		});

		BotonImportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/explorar.png"))); // NOI18N
		BotonImportar.setText("Explorar");
		BotonImportar.setDescription("Abrir fichero XML");
		BotonImportar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				Importar(evt);
			}
		});

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Controles de la Tabla", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), java.awt.Color.blue)); // NOI18N

		BotonAñadir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/add.png"))); // NOI18N
		BotonAñadir.setText(" ");
		BotonAñadir.setToolTipText("Añade una nueva fila a la tabla");
		BotonAñadir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AddFila(evt);
			}
		});

		BotonBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/remove.png"))); // NOI18N
		BotonBorrar.setToolTipText("Borra la fila seleccionada de la Tabla");
		BotonBorrar.setActionCommand("");
		BotonBorrar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				DeleteFila(evt);
			}
		});

		BotonBorrarTodo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/delete.png"))); // NOI18N
		BotonBorrarTodo.setToolTipText("Borra todas las filas de la tabla");
		BotonBorrarTodo.setActionCommand("");
		BotonBorrarTodo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				DeleteAll(evt);
			}
		});

		org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
				jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.add(BotonAñadir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.add(18, 18, 18)
						.add(BotonBorrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
						.add(BotonBorrarTodo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		jPanel1Layout.setVerticalGroup(
				jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
								.add(BotonAñadir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, Short.MAX_VALUE)
								.add(BotonBorrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, Short.MAX_VALUE)
								.add(BotonBorrarTodo, 0, 0, Short.MAX_VALUE))
								.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);

		MenuArchivo.setText("Archivo");

		MenuSalir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
		MenuSalir.setText("Salir");
		MenuSalir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				Salir(evt);
			}
		});
		MenuArchivo.add(MenuSalir);

		jMenuBar1.add(MenuArchivo);

		MenuTabla.setText("Tabla");

		MenuAdd.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
		MenuAdd.setText("Añadir Fila");
		MenuAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AddFila(evt);
			}
		});
		MenuTabla.add(MenuAdd);

		MenuBorrar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
		MenuBorrar.setText("Borrar Fila");
		MenuBorrar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				DeleteFila(evt);
			}
		});
		MenuTabla.add(MenuBorrar);
		MenuTabla.add(jSeparator1);

		MenuBorrarTodo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
		MenuBorrarTodo.setText("Borrar Tabla");
		MenuBorrarTodo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				DeleteAll(evt);
			}
		});
		MenuTabla.add(MenuBorrarTodo);

		jMenuBar1.add(MenuTabla);

		MenuAyuda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/help.png"))); // NOI18N

		MenuAcerca.setText("Acerca De...");
		MenuAcerca.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ClickAcercaDe(evt);
			}
		});
		MenuAyuda.add(MenuAcerca);

		MenuInformacion.setText("Informacion");
		MenuInformacion.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ClickInformacion(evt);
			}
		});
		MenuAyuda.add(MenuInformacion);

		jMenuBar1.add(MenuAyuda);

		setJMenuBar(jMenuBar1);

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
								.add(PanelTabla, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.add(layout.createSequentialGroup()
										.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
												.add(BotonImportar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
												.add(BotonExportar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE))))
												.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(PanelTabla, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout.createSequentialGroup()
										.add(12, 12, 12)
										.add(BotonExportar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
										.add(BotonImportar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(layout.createSequentialGroup()
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
		);

		addWindowListener(new java.awt.event.WindowAdapter()
		{
			public void windowClosing(java.awt.event.WindowEvent evt)
			{
				Salir(null);
			}
		});
		pack();
	}                       

	private void crearTabla()
	{
		dtm = new DefaultTableModel();
		dtm.addColumn("Color");
		dtm.addColumn("Texto");
		dtm.addColumn("Sonido");
		dtm.addColumn("Tiempo");
		tabla = new JTable(dtm);
		ScrollPane.setViewportView(tabla);
	}

	public void addEnsayo(Object[] ensayo)
	{
		dtm.addRow(ensayo);
	}
	
	private void Salir(java.awt.event.ActionEvent evt) 
	{                       
		controller.salir();
	}                      

	private void AddFila(java.awt.event.ActionEvent evt) 
	{                         
		controller.addFila();
	}                        

	private void DeleteFila(java.awt.event.ActionEvent evt) 
	{                            
		if(tabla.getSelectedRowCount()!=0)
		{
			int [] aux = tabla.getSelectedRows();
			for(int i=0; i<aux.length;i++)
			{
				dtm.removeRow(aux[(aux.length-1)-i]);
			}
		}
		else
		{
			JOptionPane.showMessageDialog(this,"Seleccione una fila para eliminarla de la tabla");
		}
	}                           

	private void DeleteAll(java.awt.event.ActionEvent evt)
	{                           
		this.crearTabla();
		this.PanelTabla.updateUI();
	}                          

	private void ClickAcercaDe(java.awt.event.ActionEvent evt) 
	{                               
		JOptionPane.showMessageDialog(this,"Not implemented yet");
	}                              

	private void ClickInformacion(java.awt.event.ActionEvent evt) 
	{                                  
		JOptionPane.showMessageDialog(this,"Not implemented yet");
	}                                 

	private void Exportar(java.awt.event.ActionEvent evt)
	{                          
		Vector tabla = this.dtm.getDataVector();
		if(!tabla.isEmpty())
		{
			boolean archivoGenerado = XMLManager.getInstance().generarExperimentoXML(tabla);
			if(!archivoGenerado)
				JOptionPane.showMessageDialog(this,"Error de programa, no se pudo generar el archivo XML");
		}else
		{
			JOptionPane.showMessageDialog(this,"La tabla está vacía, no se puede generar un experimento vacio");
		}

	}                         

	private void Importar(java.awt.event.ActionEvent evt) 
	{                          
		File archivo = FileManager.getInstance().abrirArchivo();
		if(archivo == null)
			return;
		ensayos = XMLManager.getInstance().leerExperimentoXML(archivo);
		if(ensayos==null)
			return;
		crearTabla();
		Object [] ensayo = new Object[4];
		for(int i=0; i < ensayos.size(); i++)
		{
			Element e = (Element) ensayos.get(i);
			ensayo[0] = e.getAttributeValue("color");
			ensayo[1] = e.getAttributeValue("texto");
			ensayo[2] = e.getAttributeValue("sonido");
			ensayo[3] = e.getAttributeValue("tiempo");
			this.addEnsayo(ensayo);
		}
	}                        

	private org.edisoncor.gui.button.ButtonIcon BotonAñadir;
	private org.edisoncor.gui.button.ButtonIcon BotonBorrar;
	private org.edisoncor.gui.button.ButtonIcon BotonBorrarTodo;
	private org.edisoncor.gui.button.ButtonTask BotonExportar;
	private org.edisoncor.gui.button.ButtonTask BotonImportar;
	private javax.swing.JMenuItem MenuAcerca;
	private javax.swing.JMenuItem MenuAdd;
	private javax.swing.JMenu MenuArchivo;
	private javax.swing.JMenu MenuAyuda;
	private javax.swing.JMenuItem MenuBorrar;
	private javax.swing.JMenuItem MenuBorrarTodo;
	private javax.swing.JMenuItem MenuInformacion;
	private javax.swing.JMenuItem MenuSalir;
	private javax.swing.JMenu MenuTabla;
	private javax.swing.JPanel PanelTabla;
	private javax.swing.JScrollPane ScrollPane;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPopupMenu.Separator jSeparator1;                 
	private JTable tabla;
	private DefaultTableModel dtm;
}
